package com.longrise.android.byjk.plugins.course.mycourse;

import android.graphics.Color;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseMultiItemQuickAdapter;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.longrise.android.byjk.R;
import com.longrise.android.byjk.model.AllCourseChildBean;
import com.longrise.android.byjk.model.CourseDefaultItem;
import com.longrise.common.utils.AppUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by callxkj on 2017/9/29.
 * 我的课程的adapter
 */

public class MyCourseDetailAdapter extends BaseMultiItemQuickAdapter<CourseDefaultItem, BaseViewHolder> {

    private OnItemClickListener mListener;
    private List<CourseDefaultItem> mCourseDefaultItemList = new ArrayList<>();

    public MyCourseDetailAdapter(List<CourseDefaultItem> data) {
        super(data);
        addItemType(CourseDefaultItem.TYPE1,R.layout.by_default_course_item);
        addItemType(CourseDefaultItem.TYPE2,R.layout.item_training_management_my);
    }

    public void refreshData(List<CourseDefaultItem> childBeanList) {
        mCourseDefaultItemList.clear();
        mCourseDefaultItemList.addAll(childBeanList);
        setNewData(childBeanList);
    }

    @Override
    protected void convert(BaseViewHolder helper, CourseDefaultItem courseDefaultItem) {
        if(CourseDefaultItem.TYPE1 == courseDefaultItem.getItemType()){
            return;
        }

        final AllCourseChildBean item = courseDefaultItem.getCourseChildBean();
        final View convertView = helper.getConvertView();

        //图片
        ImageView imageView = helper.getView(R.id.item_train_my_iv);
        String realpic = item.getRealpic();
        Glide.with(mContext)
                .load(realpic)
                .placeholder(mContext.getResources().getDrawable(R.drawable.img03))
                .crossFade()
                .into(imageView);
        Glide.get(mContext).clearMemory();


        //------标题---------
        String name = item.getName();
        final TextView tvTitle = helper.getView(R.id.item_train_my_tv1);
        tvTitle.setText(name == null ? "" : name);

        tvTitle.post(new Runnable() {
            @Override
            public void run() {
                int lineCount = tvTitle.getLineCount();
                ViewGroup.LayoutParams layoutParams = convertView.getLayoutParams();
                if (lineCount >= 2) {
                    layoutParams.height = AppUtil.dip2px(126);
                } else {
                    layoutParams.height = AppUtil.dip2px(108);
                }
                convertView.setLayoutParams(layoutParams);
            }
        });

        //有效时间
        String expiretime = item.getExpiretime();
        TextView tvDate = helper.getView(R.id.item_train_my_tv2);

        tvDate.setText(expiretime == null || expiretime.equals("") ? "" : "有效期至\u0020" + expiretime.substring(0, 10));

        //是否是已下架的产品
        String coursestate = item.getCoursestate();
        TextView tvHdjs = helper.getView(R.id.item_train_my_status_hdjs);
        if ("2".equals(coursestate)) {  //0.正常  1.已过期  2.已下架
            tvHdjs.setVisibility(View.VISIBLE);
        } else {
            tvHdjs.setVisibility(View.GONE);
        }

        //是否通过
        boolean ispass = item.ispass();
        TextView tvstatus = helper.getView(R.id.item_train_my_status_tv);
        helper.setText(R.id.item_train_my_status_tv, ispass ? "已通过" : "未通过");
        tvstatus.setTextColor(ispass ? Color.parseColor("#D8D8D8") : Color.parseColor("#FBA140"));
        convertView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mListener != null) {
                    mListener.onClick(item);
                }
            }
        });

        //让最后的item 分割线不显示
        int layoutPosition = helper.getLayoutPosition();
        if (layoutPosition == mCourseDefaultItemList.size()) {
            helper.getView(R.id.item_train_my_divider2).setVisibility(View.GONE);
        } else {
            helper.getView(R.id.item_train_my_divider2).setVisibility(View.VISIBLE);
        }
    }


    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mListener = listener;
    }

    public interface OnItemClickListener {
        void onClick(AllCourseChildBean item);
    }
}
